package edu.jxut.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.jxut.reggie.common.R;
import edu.jxut.reggie.pojos.Employee;
import edu.jxut.reggie.server.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工新增
     * @param employee
     * @param request
     * @return
     */
    @PostMapping
    public R<String> saveEmployee(@RequestBody Employee employee,HttpServletRequest request){

        log.info("正在插入数据{}",employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        //获得当前登录用户的id
        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);

        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    /**
     * 员工登录
     * @param request
     * @param param
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Map<String,String> param){
        //1、将页面提交的密码password进行md5加密处理
        String password = param.get("password");
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,param.get("username"));
        Employee emp = employeeService.getOne(queryWrapper);

        //3、如果没有查询到则返回登录失败结果
        if(emp == null){
            return R.error("登录失败");
        }

        //4、密码比对，如果不一致则返回登录失败结果
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }

        //5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if(emp.getStatus() == 0){
            return R.error("账号已禁用");
        }

        //6、登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    /**
     * 分页查询
     * @param page 当前页数
     * @param pageSize 页大小
     * @param userName 用户名
     * @return
     */
    @GetMapping("/page")
    public R<Page> queryByList(
            @RequestParam(value = "page") int page
            ,@RequestParam(value = "pageSize") int pageSize
            ,@RequestParam(value = "name",required = false)String userName){

        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,userName);

        Page<Employee> employeePage = employeeService.selectListPage(page, pageSize, userName);

        return R.success(employeePage);
    }

    /**
     * 修改状态&修改信息
     * @param request
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){

        Long id = (Long) request.getSession().getAttribute("employee");
        if (employee.getUpdateUser()==null){
            //为null表示为禁用！
            Employee result = employeeService.getById(employee.getId());
            Integer status = result.getStatus();
            //查询数据库查看数据是否为禁用
            int statusInt= status==0 ? 1:0;
//            employee.setUpdateTime(LocalDateTime.now());
//            employee.setUpdateUser(id);
            employee.setStatus(statusInt);
            employeeService.updateById(employee);
            return R.success("修改成功！");
        }

//        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改成功！");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable("id") Long id){

        Employee employee = employeeService.getById(id);
        return R.success(employee);

    }

    @DeleteMapping
    public R<String> delete(@RequestParam("ids") String ids){

        employeeService.removeById(ids);
        return R.success("删除成功");
    }

    /**
     * 修改密码
     * @param request
     * @param employee
     * @return
     */
    @PutMapping("/updatePassword")
    public R<String> updatePassword(HttpServletRequest request
            ,@RequestBody Employee employee){

        Long id = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(id);
//        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

}